package com.franklin.validation.core.params;


import com.franklin.validation.core.annotations.NotNullForMap;
import com.franklin.validation.core.annotations.ParamCheck;
import com.franklin.validation.core.annotations.RequestParamRule;
import com.franklin.validation.core.constants.Constants;
import com.franklin.validation.core.entity.CheckJsonEntity;
import com.franklin.validation.core.interfaces.Rule;
import com.franklin.validation.core.utils.DataCheckUtil;
import com.franklin.validation.core.utils.ReflectionUtil;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * Description :
 * Create By: Franklin
 * Date : 2020/6/1 16:52
 */
@Component
@RequestParamRule(version = "1")
@Deprecated
public class RequestParamRuleV1 implements Rule<CheckJsonEntity,String> {

    @Override
    public String check(CheckJsonEntity input) {
        Map<String,Object> params = input.getParams();
        NotNullForMap notNullForMap = input.getNotNullForMap();
        try {
            Arrays.stream(notNullForMap.params())
                    .forEach(paramCheck -> {
                        Object param = params.get(paramCheck.value());
                        boolean isEmpty = DataCheckUtil.isEmpty(param);
                        boolean isSysDefault = ReflectionUtil.isSystemDefaultValue(paramCheck.defaultValue());
                        boolean patternPass = false;
                        boolean lengthPass = false;
                        boolean hasPattern = DataCheckUtil.isNotEmpty(paramCheck.pattern());
                        boolean hasMessage = DataCheckUtil.isNotEmpty(paramCheck.message());
                        if (String.class == paramCheck.type() || Date.class == paramCheck.type()){
                            param = DataCheckUtil.trim(param);
                        }
                        if (!isEmpty && hasPattern && String.class == paramCheck.type()){
                            patternPass = Pattern.compile(paramCheck.pattern()).matcher((String)param).matches();
                        }
                        if (!hasPattern){
                            patternPass = true;
                        }
                        if (!isEmpty && String.class.isInstance(param)){
                            String str = (String) param;
                            if (str.length() <= paramCheck.max() && str.length() >= paramCheck.min()){
                                lengthPass = true;
                            }
                        }else {
                            lengthPass = true;
                        }
                        if (paramCheck.required()){
                            //必填
                            if (isEmpty){
                                //为空，判断有没有默认值
                                if (isSysDefault){
                                    if (hasMessage){
                                        throw new RuntimeException(Constants.MESSAGE + paramCheck.message());
                                    }
                                    throw new RuntimeException(paramCheck.value());
                                }
                                params.put(paramCheck.value(),paramCheck.defaultValue());
                            } else
                                checkPatternAndLength(params, paramCheck, isSysDefault, patternPass, lengthPass, hasMessage);
                        }else {
                            //非必填
                            if (isEmpty){
                                //为空判断是否有默认值
                                if (isSysDefault){
                                    params.remove(paramCheck.value());
                                }else {
                                    params.put(paramCheck.value(),paramCheck.defaultValue());
                                }
                            }else {
                                checkPatternAndLength(params, paramCheck, isSysDefault, patternPass, lengthPass, hasMessage);
                            }
                        }
                    });
        } catch (Exception e) {
            return e.getMessage();
        }
        return Constants.SUCCESS_STR;
    }

    private void checkPatternAndLength(Map<String, Object> params, ParamCheck paramCheck, boolean isSysDefault, boolean patternPass, boolean lengthPass, boolean hasMessage) {
        if (!patternPass || !lengthPass){
            if (isSysDefault){
                if (hasMessage){
                    throw new RuntimeException(Constants.MESSAGE + paramCheck.message());
                }
                throw new RuntimeException(paramCheck.value());
            }
            params.put(paramCheck.value(),paramCheck.defaultValue());
        }
    }
}
